package com.example.mydianping.controller;

import com.example.mydianping.dto.LoginFormDTO;
import com.example.mydianping.dto.Result;
import com.example.mydianping.service.UserService;
import com.example.mydianping.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    //用户签到
    @PostMapping("sign")
    public Result userSign(){
        return userService.userSign();
    }

    @PostMapping("/code")//向用户的手机号发送验证码
    public Result sendCode(@RequestParam("phone") String phone, HttpSession session){
        return userService.sendCode(phone,session);
    }

    @PostMapping("/login")//请求会传递过来一个 json，代表登录的信息
    public Result login(@RequestBody LoginFormDTO loginFormDTO, HttpSession session){
        return userService.login(loginFormDTO,session);
    }

    @GetMapping("/me")//获取当前的 user 并返回
    public Result me(){
        //该请求通过拦截器后，线程中会保存 user ，直接返回即可
        return Result.ok(UserHolder.getUser());
    }

}
